//app.js
const openIdUrl = require('./config').openIdUrl
import WxRequest from '/utils/wx-request/lib/index.js'

App({
    onLaunch: function() {
        this.globalData.userInfo = wx.getStorageSync('userInfo');

        var that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.globalData.windowHeight = res.windowHeight;
                that.globalData.windowWidth = res.windowWidth;
            }
        });

        console.log("执行onLaunch")

        console.log("初始化数据字典")

        wx.setStorageSync("dataDrict", {
            order_status_10: '待付款',
            order_status_20: '待接单',
            order_status_30: '待发货',
          }
        );

        this.WxRequest.interceptors.use({
            // 请求数据
            request(request) {
                let sessionId = wx.getStorageSync('sessionId');
                if (sessionId == '') {
                    sessionId = 'unlogin';
                }
                console.log('sessionId:', sessionId);
                console.log('request:', request);
                request.header.sessionId = sessionId;
                wx.showLoading({
                    title: '加载中...',
                })
                return request
            },
            // 请求失败
            requestError(requestError) {
                wx.hideLoading()
                wx.showModal({
                    content: '服务器异常,请稍候再试',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {

                        }
                    }
                });
                return Promise.reject(requestError)
            },
            // 响应数据
            response(response) {
                wx.hideLoading()
                console.log('respon:', response);
                if (response.data.code == '-1') {
                    wx.redirectTo({
                        url: '/pages/my_login/index',
                    })
                }
                return response
            },
            // 响应失败
            responseError(responseError) {
                wx.hideLoading()
                wx.showModal({
                    content: '服务器异常,请稍候再试',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {

                        }
                    }
                });
                return Promise.reject(responseError)
            },
        })

    },
    
    WxRequest: new WxRequest({
        baseURL: 'http://127.0.0.1:8910',
    }),

    globalData: {
        userInfo: null,
        windowWidth:0,
        windowHeight:0
    },
    server: {
        // addr: "http://192.168.2.185:8910"
        // addr: "https://tddtest.zcjb.com.cn"
        addr: "http://127.0.0.1:8910"
    },
    dataDict:{
        order_status:{
            "10":"待付款",
            "20":"待接单",
            "30":"待发货",
        }
    }

})